package org.sixtythousand.joker.handler;

import org.sixtythousand.joker.Request;
import org.sixtythousand.joker.Response;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author zhoulk
 * @date 2023/10/12 16:19
 * @desc
 */
public abstract class AbstractHandlerManager implements HandlerManager {


    protected Collection<HandlerWrapper> handlers;

    public AbstractHandlerManager() {
        handlers = new ArrayList<>();
    }


    @Override
    public void handle(Request request, Response response) {
        handlers.stream()
                .filter(handler -> handler.match(request))
                .forEach(handler -> {
                    if (request.isHandled()) {
                        return;
                    }
                    handler.handle(request, response);
                });
    }

    @Override
    public void addHandlers(HandlerWrapper... handlers) {
        for (HandlerWrapper handler : handlers) {
            this.handlers.add(handler);
        }
    }

    @Override
    public void removeHandlers(HandlerWrapper... handlers) {

    }
}
